

from zoom import zoomer

import dzentity, dzpage, dzutil

#------------------------------------------------------------------------
class SystemLog(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('select * from dz_log order by date desc, time desc limit 100')

    def get_LIST(self):
        labels = ['ID','App','Action','Status','User','Address','Date','Time']
        items = [(i.entryid,i.product,i.pageid,i.status,i.user,i.remoteaddr,i.date,i.time) for i in self.data]
        return dzutil.browse(items,labels=labels)

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index_system_log.txt',self))
        return page.render()

#------------------------------------------------------------------------
class TableIndex(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('show tables')

    def get_LIST(self):
        def rec_count(table_name):
            d = zoomer.db('select count(*) from %s' % table_name)
            return d[0][0]

        labels = ['Table Name','Records']
        items = [(i[0],i[0],rec_count(i[0])) for i in self.data]
        return dzutil.browse(items,on_click='show_table',labels=labels)

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index_tables.txt',self))
        return page.render()

#------------------------------------------------------------------------
class ShowTable(dzentity.Entity):
    def __init__(self,table_name):
        self.data = zoomer.db('describe %s'%table_name)
        self.table_name = table_name

    def get_NAME(self):
        return self.table_name

    def get_FIELD_LIST(self):
        def rec_count(table_name):
            d = zoomer.db('select count(*) from %s' % table_name)
            return d[0][0]

        labels = ['Field','Type','Null','Key','Default','Extra']
        items = [(i.FIELD,i.TYPE,i.NULL,i.KEY,i.DEFAULT,i.EXTRA) for i in self.data]
        return dzutil.browse(items,labels=labels)

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index_show_table.txt',self))
        return page.render()

#------------------------------------------------------------------------
class UserLog(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('select user, max(timestamp(date,time)) dt, count(*) as cnt from dz_log group by user order by cnt desc')

    def get_LIST(self):
        labels = ['User','Last Seen','Page Count']
        items = [(i.user,dzutil.how_long_ago(i.dt),i.cnt) for i in self.data]
        return dzutil.browse(items,labels=labels)

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index_users.txt',self))
        return page.render()

#------------------------------------------------------------------------
class AddressLog(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('select remoteaddr, count(*) as cnt from dz_log group by remoteaddr order by cnt desc')

    def get_LIST(self):
        labels = ['Address','Page Count']
        items = [(i.remoteaddr,i.cnt) for i in self.data]
        return dzutil.browse(items,labels=labels)

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index_addresses.txt',self))
        return page.render()

#------------------------------------------------------------------------
class SessionLog(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('select * from dz_sessions order by expiry desc limit 100')

    def get_LIST(self):
        labels = ['Expiry','Status','Value']
        items = [(i.expiry,i.status,i.value) for i in self.data]
        return dzutil.browse(items,labels=labels)

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index_sessions.txt',self))
        return page.render()


class Environment(dzentity.Entity):
    def get_ENVIRONMENT(self):
        fmt = '%-20s: %s<br>\n'
        x = [(fmt % (k,v)) for k,v in zoomer.environ.items()]
        return ''.join(x)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index_environment.txt',self))
        return page.render()



#------------------------------------------------------------------------
class Index(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('select count(*) as cnt from dz_log')
        self.log_count = self.data[0].CNT
        self.data = zoomer.db('select distinct user from dz_log')
        self.user_count = self.data.rowcount
        self.data = zoomer.db('select count(*) as cnt from dz_sessions')
        self.session_count = self.data[0].CNT
        self.data = zoomer.db('show tables')
        self.table_count = self.data.rowcount

    def get_LOG_COUNT(self):
        return self.log_count

    def get_USER_COUNT(self):
        return self.user_count

    def get_SESSION_COUNT(self):
        return self.session_count

    def get_TABLE_COUNT(self):
        return self.table_count

    def get_SYSTEM_VARIABLES(self):
        keys = ['sid','use_cookies','site_url','root_url','main_url','theme_url']
        result = []
        result += ['<br><H2>System Variables</H2>']
        result += ['%s: %s<br>' % (k,zoomer.__dict__.get(k,'None')) for k in keys]
        result += ['config file: %s<br>' % zoomer.config.filename]
        import os
        result += ['cwd: %s<br>' % os.getcwd()]
        result += ['<br><H2>Paths</H2>']
        result += ['root_path: %s<br>' % zoomer.root_path]
        result += ['site_path: %s<br>' % zoomer.site_path]
        result += ['data_path: %s<br>' % zoomer.data_path]
        result += ['theme_path: %s<br>' % zoomer.theme_path]
        result += ['app_path: %s<br>' % zoomer.app_path]
        import sys
        result += ['python path: %s' % sys.path]
        import os
        result += ['<br><br><H2>Web Server</H2>']
        result += ['QUERY_STRING: %s<br>'%os.environ.get('QUERY_STRING')]
        result += ['REQUEST_URI: %s<br>'%os.environ.get('REQUEST_URI')]
        return ''.join(result)
#        return 'site_url: %s<br>root_url: %s<br>main_url: %s' % (zoomer.site_url,zoomer.root_url,zoomer.main_url)

    def render(self):
        page = dzpage.Page(dzutil.render('views/index.txt',self,folder='views'))
        return page.render()

